package com.zj.base.base_fg;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zj.base.base_act.BaseActivity;
import com.zj.base.permission.interfaces.OnPermissionCheckListener;
import com.zj.base.view_injector.ViewBinder;
import com.zj.base.views.BaseLoadingView;
import com.zj.base.views.BaseTitleView;

import java.util.Map;

/**
 * Created by ZhaoJie on 2018/7/7.
 */

public abstract class BaseFragment extends Fragment {

    private String TAG = getClass().getName() + ": case--";
    View rootView;
    BaseActivity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutId(), container, false);
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null && parent.getChildCount() > 0) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        initView();
        initData();
    }

    @SuppressWarnings("unchecked")
    protected <T extends View> T find(int id) {
        return (T) rootView.findViewById(id);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewBinder.bind(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (getActivity() instanceof BaseActivity) {
            this.mActivity = (BaseActivity) getActivity();
        } else {
            throw new NullPointerException(TAG + "you must use baseFragment with a BaseActivity context");
        }
    }

    /**
     * cheek your App Permissions
     *
     * @param listener    when permissions callBack
     * @param permissions the permissions with you wanna get,use @see PermissionTools.PermissionManifest
     */
    protected void cheekSelfPermission(Map<String, Boolean> permissions, @NonNull OnPermissionCheckListener listener) {
        if (mActivity != null) mActivity.cheekSelfPermission(permissions, listener);
    }

    protected void cheekSelfPermission(String[] permissions, boolean[] forces, @NonNull OnPermissionCheckListener listener) {
        if (mActivity != null) mActivity.cheekSelfPermission(permissions, forces, listener);
    }

    protected void cheekSelfPermission(boolean force, String[] permissions, @NonNull OnPermissionCheckListener listener) {
        if (mActivity != null) mActivity.cheekSelfPermission(force, permissions, listener);
    }

    public void hint(BaseLoadingView.DisplayMode mode, String hint, boolean showOnAct) {
        if (mActivity != null)
            mActivity.hint(mode, hint, showOnAct);
    }

    public BaseTitleView getActivityTitleBar() {
        if (mActivity != null) return mActivity.getBaseTitleView();
        throw new NullPointerException(TAG + "has your mActivity context was null?");
    }

    @Override
    public void onDestroy() {
        if (mActivity != null) mActivity = null;
        super.onDestroy();
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();
}
